var toTree = function (data,efCountMap) {
    // 删除 所有 children,以防止多次调用
    // $.each(data, function (value) {
    //     delete value.children;
    // });
    // 将数据存储为 以 id 为 KEY 的 map 索引数据列
    // var mappa = {};
    // var mapch = {};
    // $.each(data, function (index,item) {
    //     mappa[item.pname] = {pname:item.pname};
    //     mapch[item.cname] = {cname:item.cname};
    // });

    var map = {
        "computer science":{name:"computer science [" + efCountMap["computer science"] + "]",itemStyle:{normal:{label:{show:true}}}},
        "genetics": {name:"genetics [" + efCountMap["genetics"] + "]",itemStyle:{normal:{label:{show:true}}}}
    };
    var sortAarrayNames = [];
    $.each(data, function (index,item) {
        // 以当前遍历项，的pid,去map对象中找到索引的id
        var parent = map[item.pname];
        if (parent) {
            var co = efCountMap[item.cname];
            co = co?co:0;
            (parent.children || ( parent.children = [] )).push({name:item.cname + " [" + co + "]",itemStyle:{normal:{label:{show:true,position:"right"}}}});
            sortAarrayNames.push();
        } else {
            //如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到 val结果集中，作为顶级
            //map[item.pname] = {pname:item.pname};
        }
    });

    var val = []
    $.each(map,function(key,values){
        val.push(values);
    });
    return val;
};

var deepCopy = function(p, c) {
    var c = c || {};
    for (var i in p) {
        if (typeof p[i] === 'object') {
            c[i] = (p[i].constructor === Array) ? [] : {};
            deepCopy(p[i], c[i]);
        } else {
            c[i] = p[i];
        }
    }
    return c;
}

/**
 * 发送 ajax 请求
 */

//,data,efCountMap
var toTreeOne = function (pareName,isInit) {
    
	var ns = [];
    if(pareName){
        ns = pareName.split(" [");
    }else{
        return [];
    }
    
    var ukl = "fosPac/fosChild";
    
    if(isInit) {
    	ukl = "fosPac/fullView"
    }
    var isCh = false;
    
    var rsData = {
    	isChi:false,
    	tData:[]
    };
    
	var val = [];
	$.ajax({
        url: gAjax.httpUrl + ukl,
        data: {fName: ns[0]},
        dataType: "json",
        async: false,
        method: "post",
        headers: {
            'content-type': 'application/x-www-form-urlencoded',
            'Authorization': getLoginToken()
        },
        success: function (res) {
            if (10000 == res.code) {
                var result = res.data;
                debugger;
        		var map = {};
        		$.each(result, function (index,item) {
        			
        			/**
        			 * 如果有子领域
        			 */
        			if(item.type == "child") {
        				isCh = true;
        				var vales = {
        						name:item.fName + " [" + item.scholarNum + "]",
        						value:10,
        						itemStyle:{normal:{label:{show:true,position:"right"}}}
        				};
            			val.push(deepCopy(vales));
            			
        			}else if(item.type == "parent") {
        				isCh = false;
        				var vales = {
        						name:item.fName + " [" + item.scholarNum + "]",
        						value:10,
        						itemStyle:{normal:{label:{show:true,position:"top"}}},
        						cusField: 'category',
        						children: []
        				};
/*        				
        				$.each(item.child, function (indexC,itemC) {
        					var valesC = {
            						name:itemC.fName + " [" + itemC.scholarNum + "]",
            						value:10,
            						itemStyle:{normal:{label:{show:true,position:"right"}}}
            				};
        					vales.children.push(deepCopy(valesC))
        				})*/;
        				
            			val.push(deepCopy(vales));
        			}
        			
        		});
            }
        }
    });
	
	rsData.isChi = isCh;
	rsData.tData = val;
	
	return rsData;

};


var forSort = function (resultValue) {
    resultValue= resultValue.sort(
        function compareFunction(param1,param2){
            return param1.localeCompare(param2);
        }
    );

    return resultValue;
}
